package com.weetop.akly.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

public class EquipmentUtil {


    /**
     * 获取手机设备名
     *
     * @return 手机设备名
     */
    public static String getSystemDevice() {
        return Build.DEVICE;
    }


    /**
     * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
     *
     * @return 手机IMEI
     */
    public static String getIMEI(Context ctx) {
        String deviceId = "";
        try {
            TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
            if (tm != null) {
                if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    return deviceId;
                }
                deviceId = tm.getDeviceId();
                if (TextUtils.isEmpty(deviceId)) {
                    deviceId = Settings.System.getString(
                            ctx.getContentResolver(), Settings.Secure.ANDROID_ID);
                }
                return deviceId;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (TextUtils.isEmpty(deviceId)) {
                deviceId = Settings.System.getString(
                        ctx.getContentResolver(), Settings.Secure.ANDROID_ID);
            }
            return deviceId;
        }
        return deviceId;
    }
}
